package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalTargetConvert;
import com.quick.develop.business.entity.EnvironmentalTargetEntity;
import com.quick.develop.business.query.EnvironmentalTargetQuery;
import com.quick.develop.business.vo.EnvironmentalTargetVO;
import com.quick.develop.business.dao.EnvironmentalTargetDao;
import com.quick.develop.business.service.EnvironmentalTargetService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境指标目标任务
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalTargetServiceImpl extends BaseServiceImpl<EnvironmentalTargetDao, EnvironmentalTargetEntity> implements EnvironmentalTargetService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalTargetVO> page(EnvironmentalTargetQuery query) {
        IPage<EnvironmentalTargetEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalTargetConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalTargetEntity> getWrapper(EnvironmentalTargetQuery query) {
        LambdaQueryWrapper<EnvironmentalTargetEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalTargetEntity> getWrapperOptional(EnvironmentalTargetQuery query) {
        LambdaQueryWrapper<EnvironmentalTargetEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalTargetEntity::getId, query.getId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalTargetEntity::getBusinessKey, query.getBusinessKey());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getAttachId()), EnvironmentalTargetEntity::getAttachId, query.getAttachId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getYear()), EnvironmentalTargetEntity::getYear, query.getYear());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getProject()), EnvironmentalTargetEntity::getProject, query.getProject());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getTarget()), EnvironmentalTargetEntity::getTarget, query.getTarget());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getControlLimit()), EnvironmentalTargetEntity::getControlLimit, query.getControlLimit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUnit()), EnvironmentalTargetEntity::getUnit, query.getUnit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getFrequency()), EnvironmentalTargetEntity::getFrequency, query.getFrequency());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentName()), EnvironmentalTargetEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalName()), EnvironmentalTargetEntity::getResponsiblePersonalName, query.getResponsiblePersonalName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCode()), EnvironmentalTargetEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCompletionDeadline()), EnvironmentalTargetEntity::getCompletionDeadline, query.getCompletionDeadline());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCompletionDate()), EnvironmentalTargetEntity::getCompletionDate, query.getCompletionDate());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalTargetEntity::getCreatedUserCode, query.getCreatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalTargetEntity::getCreatedUserName, query.getCreatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalTargetEntity::getCreatedTime, query.getCreatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalTargetEntity::getUpdatedUserCode, query.getUpdatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalTargetEntity::getUpdatedUserName, query.getUpdatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalTargetEntity::getUpdatedTime, query.getUpdatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalTargetEntity::getDelFlag, query.getDelFlag());
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalTargetEntity::getId, query.getIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalTargetEntity::getBusinessKey, query.getBusinessKeyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getAttachIdLike()), EnvironmentalTargetEntity::getAttachId, query.getAttachIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getYearLike()), EnvironmentalTargetEntity::getYear, query.getYearLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getProjectLike()), EnvironmentalTargetEntity::getProject, query.getProjectLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getTargetLike()), EnvironmentalTargetEntity::getTarget, query.getTargetLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getControlLimitLike()), EnvironmentalTargetEntity::getControlLimit, query.getControlLimitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUnitLike()), EnvironmentalTargetEntity::getUnit, query.getUnitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getFrequencyLike()), EnvironmentalTargetEntity::getFrequency, query.getFrequencyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentNameLike()), EnvironmentalTargetEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalNameLike()), EnvironmentalTargetEntity::getResponsiblePersonalName, query.getResponsiblePersonalNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCodeLike()), EnvironmentalTargetEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCompletionDeadlineLike()), EnvironmentalTargetEntity::getCompletionDeadline, query.getCompletionDeadlineLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCompletionDateLike()), EnvironmentalTargetEntity::getCompletionDate, query.getCompletionDateLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalTargetEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalTargetEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalTargetEntity::getCreatedTime, query.getCreatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalTargetEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalTargetEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalTargetEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalTargetEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalTargetVO vo) {
        EnvironmentalTargetEntity entity = EnvironmentalTargetConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalTargetVO vo) {
        EnvironmentalTargetEntity entity = EnvironmentalTargetConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalTargetVO getEnvironmentalTargetVO(String id) {
    EnvironmentalTargetEntity entity = getById(id);
        return EnvironmentalTargetConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalTargetVO getEnvironmentalTargetByBusinessKey(String businessKey) {
        EnvironmentalTargetEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalTargetEntity>()
                .eq(EnvironmentalTargetEntity::getBusinessKey, businessKey));
        return EnvironmentalTargetConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalTargetVO> getListByKeys(EnvironmentalTargetQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalTargetQuery query) {
        List<EnvironmentalTargetEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalTargetEntity.class, list, "环境指标目标任务导出");
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalTargetEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalTargetVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalTargetEntity> entities = list.stream().map(EnvironmentalTargetConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalTargetVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalTargetEntity> entities = list.stream().map(EnvironmentalTargetConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalTargetVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalTargetConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalTargetEntity.class).in(key, keys)));
    }

    /**
    *
    * @param sFunction 函数式获取Key
    * @param keys 需要查询的列表
    * @return 返回数据列表
    */
    public List<EnvironmentalTargetEntity> getListByKeys(SFunction<EnvironmentalTargetEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalTargetEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalTargetEntity> filterDynamicImport(List<EnvironmentalTargetEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalTargetEntity> overrideDynamicImport(List<EnvironmentalTargetEntity> list) {
        return Collections.emptyList();
    }
}